//
//  XSImageClipView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/9/19.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSImageClipView.h"
#import "XSImageRatioView.h"
#import "XSImageGridView.h"
 
@interface XSImageClipView ()

//@property(nonatomic,strong)UIImageView *imageView;
//@property(nonatomic,strong)XSImageGridView *gridView;
//@property(nonatomic,strong)UIView *topLeftView;
//@property(nonatomic,strong)UIView *topRightView;
//@property(nonatomic,strong)UIView *botLeftView;
//@property(nonatomic,strong)UIView *botRightView;
//
//@property(nonatomic,strong)UIView* ratioView;
//@property(nonatomic,strong)XSImageRatioView *selectedRatioView;

@end

@implementation XSImageClipView
//
//- (UIView *)ratioView{
//    if (!_ratioView) {
//        NSArray *array = @[@{@"value1":@"0",@"value2":@"0"},
//                           @{@"value1":@"1",@"value2":@"1"},
//                           @{@"value1":@"4",@"value2":@"3"},
//                           @{@"value1":@"3",@"value2":@"2"},
//                           @{@"value1":@"16",@"value2":@"9"}];
//        CGFloat W = 70;
//        CGFloat H = 80;
//        CGFloat x = 0;
//        _ratioView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, H)];
//        [self addSubview:_ratioView];
//        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:_ratioView.bounds];
//        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//        scrollView.showsHorizontalScrollIndicator = NO;
//        [_ratioView addSubview:scrollView];
//        
//        CGSize  imgSize = self.originImage.size;
//        CGFloat maxW = MIN(imgSize.width, imgSize.height);
//        UIImage *iconImage = [self scaleImage:self.originImage toSize:CGSizeMake(W * imgSize.width/maxW, W * imgSize.height/maxW)];
//        
//        
//        for (NSDictionary *info in array) {
//            CGFloat val1 = [info[@"value1"] floatValue];
//            CGFloat val2 = [info[@"value2"] floatValue];
//            XSImageRatio *ratio = [[XSImageRatio alloc]initWithWidth:val1 height:val2];
//            ratio.isLandscape = val1 > val2;
//            
//            XSImageRatioView *ratioView = [[XSImageRatioView alloc]initWithFrame:CGRectMake(x , 0 , W, H) image:iconImage target:self action:@selector(tapRadio:)];
//            ratioView.ratio = ratio;
//            [ratioView refreshViews];
//            [scrollView addSubview:ratioView];
//            
//            x += W;
//        }
//        scrollView.contentSize = CGSizeMake(x, H);
//    }
//    return _ratioView;
//}
//
//- (UIImageView *)imageView{
//    if (!_imageView) {
//        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, self.width, self.height - 100)];
//        _imageView.contentMode = UIViewContentModeScaleAspectFill;
//        [self addSubview:_imageView];
//        
//        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGridView:)];
//        _imageView.userInteractionEnabled = YES;
//        [_imageView addGestureRecognizer:panGesture];
//    }
//    return _imageView;
//}
//- (XSImageGridView *)gridView{
//    if (!_gridView) {
//        _gridView = [[XSImageGridView alloc]initWithFrame:CGRectZero];
//        _gridView.backgroundColor = [UIColor clearColor];
//        [self.imageView addSubview:_gridView];
//    }
//    return _gridView;
//}
//- (UIView *)topLeftView{
//    if (!_topLeftView) {
//        _topLeftView = [self clippingCircleWithTag:0];
//    }
//    return _topLeftView;
//}
//- (UIView *)botLeftView{
//    if (!_botLeftView) {
//        _botLeftView = [self clippingCircleWithTag:1];
//    }
//    return _botLeftView;
//}
//- (UIView *)topRightView{
//    if (!_topRightView) {
//        _topRightView = [self clippingCircleWithTag:2];
//    }
//    return _topRightView;
//}
//
//- (UIView *)botRightView{
//    if (!_botRightView) {
//      _botRightView = [self clippingCircleWithTag:3];
//    }
//    return _botRightView;
//}
//- (UIView *)clippingCircleWithTag:(NSInteger)tag{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
//    view.layer.cornerRadius = view.width/2;
//    view.layer.masksToBounds = YES;
//    view.backgroundColor = [UIColor whiteColor];
//    view.tag = tag;
//    [self addSubview:view];
//    
//    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panCircleView:)];
//    [view addGestureRecognizer:panGesture];
//    return view;
//}
//
////#pragma mark - Action
////- (void)cancelBtnAction{
////
////}
////- (void)doneBtnAction{
////
////}
//
//#pragma mark - setter
//- (void)setOriginImage:(UIImage *)originImage{
//    _originImage = originImage;
//    self.imageView.image = originImage;
//    [self resetImageViewFrameWithImage:originImage];
//}
//- (void)resetImageViewFrameWithImage:(UIImage *)image{
//    if (!image) return;
//    CGFloat wid = kWidth - 40;
//    CGFloat maxHei = kHeight - 100 - 120;
//    
//    CGFloat hei = wid * image.size.height/image.size.width;
//    if (hei > maxHei) {
//        hei = maxHei;
//        wid = hei * image.size.width/image.size.height;
//    }
//    //init
//    self.imageView.frame = CGRectMake((kWidth - wid)/2, 100, wid, hei);
//    self.gridView.frame = self.imageView.bounds;
//    self.gridView.clipRect = self.gridView.bounds;
//    
//    CGRect clipRect = self.imageView.frame;
//    CGFloat x =  clipRect.origin.x;
//    CGFloat y =  clipRect.origin.y;
//    CGFloat wid_img = clipRect.size.width;
//    CGFloat hei_img = clipRect.size.height;
//    self.topLeftView.center = CGPointMake(x , y);
//    self.topRightView.center = CGPointMake(x + wid_img, y);
//    self.botLeftView.center = CGPointMake(x , y + hei_img);
//    self.botRightView.center = CGPointMake(x + wid_img, y + hei_img);
//    
//    //default 相对imageView的
//    _clippingRect = self.imageView.bounds;
//
//}
//
//- (void)setClippingRect:(CGRect)clippingRect{
//    _clippingRect = clippingRect;
//    
//    [self resetCornerViewFrame:clippingRect];
//    self.gridView.clipRect = clippingRect; //相对imageView的
//}
//
//- (void)resetCornerViewFrame:(CGRect)clipRect{
//    CGFloat x =  clipRect.origin.x;
//    CGFloat y =  clipRect.origin.y;
//    CGFloat wid = clipRect.size.width;
//    CGFloat hei = clipRect.size.height;
//    
//    self.topLeftView.center = [self convertPoint:CGPointMake(x , y) fromView:_imageView];
//    self.topRightView.center = [self convertPoint:CGPointMake(x + wid, y) fromView:_imageView];
//    self.botLeftView.center = [self convertPoint:CGPointMake(x , y + hei) fromView:_imageView];
//    self.botRightView.center = [self convertPoint: CGPointMake(x + wid, y + hei) fromView:_imageView];
//
//}
//
//#pragma mark - 拖动
//- (void)panCircleView:(UIPanGestureRecognizer*)sender
//{
//    CGPoint point = [sender locationInView:_imageView];
//    CGPoint dp = [sender translationInView:_imageView];
//    
//    CGRect rct = self.clippingRect;
//    
//    const CGFloat W = _imageView.frame.size.width;
//    const CGFloat H = _imageView.frame.size.height;
//    CGFloat minX = 0;
//    CGFloat minY = 0;
//    CGFloat maxX = W;
//    CGFloat maxY = H;
//    
//    CGFloat ratio = 0;
////    CGFloat ratio = (sender.view.tag == 1 || sender.view.tag==2) ? -self.selectClipRatio.ratio : self.selectClipRatio.ratio;
//    
//    switch (sender.view.tag) {
//        case 0: // upper left
//        {
//            maxX = MAX((rct.origin.x + rct.size.width)  - 0.1 * W, 0.1 * W);
//            maxY = MAX((rct.origin.y + rct.size.height) - 0.1 * H, 0.1 * H);
//            
//            if (ratio!=0) {
//                CGFloat y0 = rct.origin.y - ratio * rct.origin.x;
//                CGFloat x0 = -y0 / ratio;
//                minX = MAX(x0, 0);
//                minY = MAX(y0, 0);
//                
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//                
//                if(-dp.x*ratio + dp.y > 0){ point.x = (point.y - y0) / ratio; }
//                else{ point.y = point.x * ratio + y0; }
//            } else {
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//            }
//            
//            rct.size.width  = rct.size.width  - (point.x - rct.origin.x);
//            rct.size.height = rct.size.height - (point.y - rct.origin.y);
//            rct.origin.x = point.x;
//            rct.origin.y = point.y;
//            break;
//        }
//        case 1: // lower left
//        {
//            maxX = MAX((rct.origin.x + rct.size.width)  - 0.1 * W, 0.1 * W);
//            minY = MAX(rct.origin.y + 0.1 * H, 0.1 * H);
//            
//            if (ratio!=0) {
//                CGFloat y0 = (rct.origin.y + rct.size.height) - ratio* rct.origin.x ;
//                CGFloat xh = (H - y0) / ratio;
//                minX = MAX(xh, 0);
//                maxY = MIN(y0, H);
//                
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//                
//                if(-dp.x*ratio + dp.y < 0){ point.x = (point.y - y0) / ratio; }
//                else{ point.y = point.x * ratio + y0; }
//            } else {
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//            }
//            
//            rct.size.width  = rct.size.width  - (point.x - rct.origin.x);
//            rct.size.height = point.y - rct.origin.y;
//            rct.origin.x = point.x;
//            break;
//        }
//        case 2: // upper right
//        {
//            minX = MAX(rct.origin.x + 0.1 * W, 0.1 * W);
//            maxY = MAX((rct.origin.y + rct.size.height) - 0.1 * H, 0.1 * H);
//            
//            if (ratio!=0) {
//                CGFloat y0 = rct.origin.y - ratio * (rct.origin.x + rct.size.width);
//                CGFloat yw = ratio * W + y0;
//                CGFloat x0 = -y0 / ratio;
//                maxX = MIN(x0, W);
//                minY = MAX(yw, 0);
//                
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//                
//                if(-dp.x*ratio + dp.y > 0){ point.x = (point.y - y0) / ratio; }
//                else{ point.y = point.x * ratio + y0; }
//            } else {
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//            }
//            
//            rct.size.width  = point.x - rct.origin.x;
//            rct.size.height = rct.size.height - (point.y - rct.origin.y);
//            rct.origin.y = point.y;
//            break;
//        }
//        case 3: // lower right
//        {
//            minX = MAX(rct.origin.x + 0.1 * W, 0.1 * W);
//            minY = MAX(rct.origin.y + 0.1 * H, 0.1 * H);
//            
//            if (ratio!=0) {
//                CGFloat y0 = (rct.origin.y + rct.size.height) - ratio * (rct.origin.x + rct.size.width);
//                CGFloat yw = ratio * W + y0;
//                CGFloat xh = (H - y0) / ratio;
//                maxX = MIN(xh, W);
//                maxY = MIN(yw, H);
//                
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//                
//                if(-dp.x*ratio + dp.y < 0){ point.x = (point.y - y0) / ratio; }
//                else{ point.y = point.x * ratio + y0; }
//            } else {
//                point.x = MAX(minX, MIN(point.x, maxX));
//                point.y = MAX(minY, MIN(point.y, maxY));
//            }
//            
//            rct.size.width  = point.x - rct.origin.x;
//            rct.size.height = point.y - rct.origin.y;
//            break;
//        }
//        default:
//            break;
//    }
//    self.clippingRect = rct;
//    
//    NSLog(@"--:%@",NSStringFromCGRect(rct));
//    
//    
//}
//
//- (void)panGridView:(UIPanGestureRecognizer*)sender{
//    static BOOL dragging = NO;
//    static CGRect initialRect;
//    
//    if (sender.state==UIGestureRecognizerStateBegan) {
//        CGPoint point = [sender locationInView:_imageView];
//        dragging = CGRectContainsPoint(_clippingRect, point);
//        initialRect = self.clippingRect;
//    } else if(dragging) {
//        CGPoint point = [sender translationInView:_imageView];
//        CGFloat left  = MIN(MAX(initialRect.origin.x + point.x, 0), _imageView.frame.size.width-initialRect.size.width);
//        CGFloat top   = MIN(MAX(initialRect.origin.y + point.y, 0), _imageView.frame.size.height-initialRect.size.height);
//        
//        CGRect rct = self.clippingRect;
//        rct.origin.x = left;
//        rct.origin.y = top;
//        self.clippingRect = rct;
//    }
//}
//
//#pragma mark - 裁剪
//- (UIImage *)clipImage
//{
//    CGFloat zoomScale = _imageView.bounds.size.width / _imageView.image.size.width;
//    CGRect rct = CGRectEqualToRect(self.clippingRect, CGRectZero) ? _imageView.bounds : self.clippingRect;
//    rct.size.width  /= zoomScale;
//    rct.size.height /= zoomScale;
//    rct.origin.x    /= zoomScale;
//    rct.origin.y    /= zoomScale;
//    
//    CGPoint origin = CGPointMake(-rct.origin.x, -rct.origin.y);
//    UIImage *img = nil;
//    
//    UIGraphicsBeginImageContextWithOptions(rct.size, NO, _imageView.image.scale);
//    [_imageView.image drawAtPoint:origin];
//    img = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    return img;
//}
//
//- (void)tapRadio:(UITapGestureRecognizer *)sender{
//    self.selectedRatioView = (XSImageRatioView *)sender.view;
//    
//}
//
//- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size{
//    
//    NSData *data = UIImageJPEGRepresentation(image, 1.0);
//    return [self scaledlmageWithData:data withSize:size scale:1.0 orientation:image.imageOrientation];
//}
//
//- (UIImage *)scaledlmageWithData:(NSData *)data withSize:(CGSize)size scale:(CGFloat)scale orientation:(UIImageOrientation)orientation {
//    CGFloat maxPixelSize = MAX(size.width, size.height);
//    CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil);
//    NSDictionary *options = @{(__bridge id)kCGImageSourceCreateThumbnailFromImageAlways:(__bridge id)kCFBooleanTrue,
//                              (__bridge id)kCGImageSourceThumbnailMaxPixelSize:[NSNumber numberWithFloat:maxPixelSize]};
//    CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options);
//    UIImage *resultlmage = [UIImage imageWithCGImage:imageRef scale:scale orientation:orientation];
//    CGImageRelease(imageRef);
//    CFRelease(sourceRef);
//    return resultlmage;
//}




@end
